package com.wei.tang.db;

import java.util.ArrayList;
import java.util.List;

import com.wei.tang.bean.UserBean;
import com.wei.tang.db.DBInfo.UserTable;
import com.wei.tang.util.Tlog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.test.AndroidTestCase;

/**
 * 
 * @author 吴佳明
 *
 */
public class CommentDao extends AndroidTestCase {

	private DBHelper dbHelper = null;
	private SQLiteDatabase db = null;
	private ContentValues values = null;
	String[] columns = { DBInfo.UserTable.USER_NAME };

	public CommentDao(Context context) {
		Tlog.i("进入数据库类");
		dbHelper = new DBHelper(context);
		Tlog.i("数据库  表创建成功");
	}

	/**
	 * 向用户表中插入单条数据，成功返回true 失败返回false
	 * 
	 * @param userBean
	 * @return
	 */
	public boolean insertUser(UserBean userBean) {
		Tlog.i("插入用户数据到数据库中");
		db = dbHelper.getWritableDatabase();
		values = new ContentValues();
		values.put(DBInfo.UserTable.USER_ID, userBean.getUser_id());
		values.put(DBInfo.UserTable.USER_NAME, userBean.getUser_name());
		values.put(DBInfo.UserTable.USER_SEX, userBean.getUser_sex());
		values.put(DBInfo.UserTable.USER_SESSION, userBean.getUser_session());
		values.put(DBInfo.UserTable.USER_STATUS, userBean.getUser_status());
		values.put(DBInfo.UserTable.DECRIPTION, userBean.getDecription());
		values.put(DBInfo.UserTable.USER_LOCATION, userBean.getUser_location());
		values.put(DBInfo.UserTable.USER_TIME, userBean.getUser_time());
		values.put(DBInfo.UserTable.USER_HEAD, userBean.getUser_head());
		long rowId = db.insert(DBInfo.UserTable.USER_TABLE, DBInfo.UserTable.USER_NAME, values);
		db.close();
		Tlog.i("是否存成功：" + rowId);
		if (rowId == -1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 更新用户数据
	 * 
	 * @param userBean
	 * @return
	 */
	public int updateUserInfo(UserBean userBean) {
		Tlog.i("更新用户信息");
		db = dbHelper.getReadableDatabase();
		values = new ContentValues();
		values.put(DBInfo.UserTable.USER_ID, userBean.getUser_id());
		values.put(DBInfo.UserTable.USER_NAME, userBean.getUser_name());
		values.put(DBInfo.UserTable.USER_SEX, userBean.getUser_sex());
		values.put(DBInfo.UserTable.USER_SESSION, userBean.getUser_session());
		values.put(DBInfo.UserTable.USER_STATUS, userBean.getUser_status());
		values.put(DBInfo.UserTable.DECRIPTION, userBean.getDecription());
		values.put(DBInfo.UserTable.USER_LOCATION, userBean.getUser_location());
		values.put(DBInfo.UserTable.USER_TIME, userBean.getUser_time());
		values.put(DBInfo.UserTable.USER_HEAD, userBean.getUser_head());
		int rowId = db.update(DBInfo.UserTable.USER_TABLE, values, "user_id=?", new String[] { userBean.getUser_id() });
		return rowId;
	}

	public String[] findAllUsers() {
		db = dbHelper.getReadableDatabase();
		String usernamelast[] = null;
		String sql = "select c.user_name from userinfo c where 5 > (select count(*) from userinfo where  user_time > c.user_time ) order by c.user_time ";
		Cursor cursor = db.rawQuery(sql, null);
		int i = 0;
		if (cursor != null && cursor.getCount() > 0) {
			usernamelast = new String[cursor.getColumnCount()];
			while (cursor.moveToNext()) {
				usernamelast[i++] = cursor.getString(0);
			}
		}
		cursor.close();
		db.close();
		return usernamelast;
	}

	/**
	 * 通过user_name查找用户 查不到会抛空指针异常
	 * 
	 * @param user_id
	 * @return
	 */
	public UserBean findUserByUserName(String user_name) {
		String sql = "SELECT * FROM userinfo WHERE user_name=?";
		String[] strs = { user_name };
		return findUserBySql(sql, strs);
	}

	/**
	 * 通过user_id查找用户
	 * 
	 * @param user_id
	 * @return
	 */
	public UserBean findUserByUserId(String user_id) {
		String sql = "SELECT ＊ FROM userinfo WHERE user_id=?";
		String[] strs = { user_id };
		return findUserBySql(sql, strs);
	}

	/**
	 * 查找年纪为user_age的用户
	 * 
	 * @param user_id
	 * @return
	 */
	public List<UserBean> findUsersByUserage(String user_age) {
		String sql = "SELECT * FROM userinfo WHERE user_age=?";
		String[] strs = { user_age };
		return findUsersBySql(sql, strs);
	}

	/**
	 * 查找名字中含有含有like_name 的用户
	 * 
	 * @param user_id
	 * @return
	 */
	public List<UserBean> findUserByLikeName(String like_name) {
		String sql = "SELECT ＊ FROM userinfo WHERE user_name like ";
		String[] strs = { "*" + like_name + "*" };
		return findUsersBySql(sql, strs);
	}

	public List<UserBean> findUsersBySql(String sql, String[] strs) {
		db = dbHelper.getReadableDatabase();
		Cursor cur = db.rawQuery(sql, strs);

		List<UserBean> userBeans = new ArrayList<UserBean>();
		while (cur.moveToNext()) {
			UserBean userBean = fillUserBean(cur.getExtras());
			userBeans.add(userBean);
		}
		db.close();
		return userBeans;
	}

	public UserBean findUserBySql(String sql, String[] strs) {
		db = dbHelper.getReadableDatabase();
		Cursor cur = db.rawQuery(sql, strs);

		UserBean userBean = null;
		if (cur.moveToNext()) {
			userBean = fillUserBean(cur.getExtras());
		}
		db.close();
		return userBean;
	}

	private UserBean fillUserBean(Bundle bundle) {
		UserBean userBean = new UserBean();
		userBean.setUser_id(bundle.getString(UserTable.USER_ID));
		userBean.setUser_name(bundle.getString(UserTable.USER_NAME));
		userBean.setUser_sex(bundle.getString(UserTable.USER_SEX));
		userBean.setUser_session(bundle.getString(UserTable.USER_SESSION));
		userBean.setUser_status(bundle.getString(UserTable.USER_STATUS));
		userBean.setDecription(bundle.getString(UserTable.DECRIPTION));
		userBean.setUser_time(bundle.getString(UserTable.USER_TIME));
		userBean.setUser_head(bundle.getString(UserTable.USER_HEAD));
		userBean.setUser_location(bundle.getString(UserTable.USER_LOCATION));
		return userBean;
	}

}
